// firstPackage/pages/loansDetailList/loansDetailList.js
let app = getApp();
import calcute from "../../../tools/calculateLoan"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    loanData: {},
    refundType: 1,//1:等额本息 2:等额本金
    switchArr: [
      {
        title: "等额本息",
        key: 1
      },
      {
        title: "等额本金",
        key: 2
      }
    ],
    //每月还款:
    returnMonthMoney: 0,
    //贷款总额
    loanTotalNum: "0万",
    //利息总额
    interestTotalNum: "0万",
    //贷款年限
    loansYear: "0年",
    loansList: [

    ],
    //减少:
    remindReduceMoney: 0,//每月递减金额
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("JJLog:接受参数是:" + JSON.stringify(options));
    this.setData({
      loanData: JSON.parse(options.loanData)
    })
    console.log(this.data.loanData);
    this.setNavBarTitle(this.data.loanData.loansType);
    if (this.data.loanData.loansType != 2) {
      //商贷和公积金贷
      this.calculateResult();
    } else {
      //组合贷计算:
      this.groupCalculateResult();
    }

  },




  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
  },
  //顶部还款方式回调:
  switchAction(e) {
    let index = e.currentTarget.dataset.index;
    let key = 1;
    if (index == 0) {
      key = 1
    } else {
      key = 2
    }
    this.setData({
      refundType: key
    })
    if (this.data.loanData.loansType != 2) {
      //商贷和公积金贷
      this.calculateResult();
    } else {
      //组合贷计算:
      this.groupCalculateResult();
    }
  },

  //结果赋值:
  resultObjDeal(resultObj) {
    //每月还款:
    // returnMonthMoney: 3444.33,
    //   //贷款总额
    //   loanTotalNum: "70万",
    //     //利息总额
    //     interestTotalNum: "34.5万",
    //       //贷款年限
    //       loansYear: "30年",
    this.setData({
      returnMonthMoney: resultObj.yuegong.toFixed(2),
      loanTotalNum: resultObj.totalDknum + "万",
      interestTotalNum: (resultObj.totalLixi / 10000).toFixed(2) + "万",
      loansYear: resultObj.year + "年",
      loansList: resultObj.monthdataArray
    })
    if (this.data.refundType == 2) {
      this.setData({
        remindReduceMoney: resultObj.yuegongdijian
      })
    }
  },
  //商贷或者公积金贷计算:
  calculateResult() {
    let data = this.data.loanData;
    let money = data.loansType == 0 ? data.businessMoney : data.fundMoney;
    let year = data.loansType == 0 ? data.businessYear : data.fundYear;
    let rate = data.loansType == 0 ? data.businessRate : data.fundRate;
    let resultObj = calcute.singleDk(this.data.refundType, money, year, rate);
    console.log("JJLog:结果是:" + JSON.stringify(resultObj));
    this.resultObjDeal(resultObj);
  },
  //组合贷计算:
  groupCalculateResult() {
    let data = this.data.loanData;
    let businessMoney = data.businessMoney;
    let fundMoney = data.fundMoney;
    let businessYear = data.businessYear;
    let fundYear = data.fundYear;
    let businessRate = data.businessRate;
    let fundRate = data.fundRate;
    console.log("JJLog:传过去的值是:" + JSON.stringify(data));

    // type, sdnum, gjjnum, sdyear, gjjyear, sdlilv, gjjlilv
    /**
     * type : 等额本息或者等额本金
     * sdNum: 商贷金额(万)
     * gjjNum: 公积金金额(万)
     * sdYear: 商贷年限
     * gjjYear: 公积金年限
     * sdlilv: 商贷利率
     * gjjlilv: 公积金利率
     */
    let resultObj = calcute.zuhe(this.data.refundType, businessMoney, fundMoney, businessYear, fundYear, businessRate, fundRate)
    console.log("JJLog:结果是:" + JSON.stringify(resultObj));
    this.resultObjDeal(resultObj);
  },


















  //navBar标题处理
  setNavBarTitle(type) {
    let title = "";
    if (type == 0) {
      title = "商业贷"
    } else if (type == 1) {
      title = "公积金贷"
    } else {
      title = "组合贷"
    }
    wx.setNavigationBarTitle({
      title: title,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },


})